/*!
* @file Color.cpp
* @brief 颜色
* @date 2024/9/13
*/
#include "Color.h"

using namespace data;

Color::Color(float red, float green, float blue, float alpha)
  : r(red), g(green), b(blue), a(alpha)
{
}

Color::Color(int rgba) : Color()
{
    SetRGBA(rgba);
}

bool Color::operator==(const Color& color) const
{
    return GetRGBA() == color.GetRGBA();
}

bool Color::operator!=(const Color& color) const
{
    return !operator==(color);
}

void Color::Set(float red, float green, float blue, float alpha)
{
    r = red;
    g = green;
    b = blue;
    a = alpha;
}

void Color::SetRGBA(int rgba)
{
    // clang-format off
    Set(static_cast<float> (rgba >> 24)         / 255.0F,
        static_cast<float>((rgba >> 16) & 0xff) / 255.0F,
        static_cast<float>((rgba >>  8) & 0xff) / 255.0F,
        static_cast<float> (rgba        & 0xff) / 255.0F);
    // clang-format on
}

int Color::GetRGBA() const
{
    // clang-format off
    return (static_cast<int>(std::lround(r * 255.0F)) << 24 |
            static_cast<int>(std::lround(g * 255.0F)) << 16 |
            static_cast<int>(std::lround(b * 255.0F)) << 8  |
            static_cast<int>(std::lround(a * 255.0F)));
    // clang-format on
}

void Color::SetRGB(int rgb)
{
    // clang-format off
    Set(static_cast<float>((rgb >> 24) & 0xff) / 255.0F,
        static_cast<float>((rgb >> 16) & 0xff) / 255.0F,
        static_cast<float>((rgb >>  8) & 0xff) / 255.0F);
    // clang-format on
}

int Color::GetRGB() const
{
    // clang-format off
    return (static_cast<int>(std::lround(r * 255.0F)) << 24 |
            static_cast<int>(std::lround(g * 255.0F)) << 16 |
            static_cast<int>(std::lround(b * 255.0F)) << 8);
    // clang-format on
}

int Color::GetARGB() const
{
    // clang-format off
    return (static_cast<int>(std::lround(a * 255.0F)) << 24 |
            static_cast<int>(std::lround(r * 255.0F)) << 16 |
            static_cast<int>(std::lround(g * 255.0F)) << 8  |
            static_cast<int>(std::lround(b * 255.0F)));
    // clang-format on
}

void Color::SetARGB(int argb)
{
    // clang-format off
    Set(static_cast<float>((argb >> 16) & 0xff) / 255.0F,
        static_cast<float>((argb >>  8) & 0xff) / 255.0F,
        static_cast<float> (argb        & 0xff) / 255.0F,
        static_cast<float> (argb >> 24)         / 255.0F);
    // clang-format on
}
